import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class QCalendarWidgetDemo(QWidget):

def __init__(self):

super(QCalendarWidgetDemo,self).__init__()

self.initUI()

def initUI(self):

self.calendar = QCalendarWidget(self) #这里没有加布局控件

# self.calendar.setGridVisible(True) #日历网格

self.calendar.setMinimumDate(QDate(1999,1,1,))

self.calendar.setMaximumDate(QDate(2099,1,1))

self.calendar.move(50,50)

self.setWindowTitle('日历控件')

self.calendar.clicked.connect(self.showDate)

self.resize(400,400)

date = self.calendar.selectedDate()

self.label = QLabel(self) #这里要加 继承关系

self.label.setText(date.toString('yyyy-MM-dd dddd'))

self.label.move(150,380)

def showDate(self):

self.label.setText((self.calendar.selectedDate().toString('yyyy-MM-dd dddd')))

if __name__ == '__main__':

app = QApplication(sys.argv)

main = QCalendarWidgetDemo()

main.show()

sys.exit(app.exec_())



#日历控件二 QDateTimeEdit

import sys

from PyQt5.QtWidgets import *

from PyQt5.QtGui import *

from PyQt5.QtCore import *

class QDateTimeEditDemo(QWidget):

def __init__(self):

super(QDateTimeEditDemo,self).__init__()

self.initUI()

def initUI(self):

self.resize(100,100)

self.datetimeEdit = QDateTimeEdit()

self.datetimeEdit.setDisplayFormat('yyyy-MM-dd HH:mm:ss')

self.datetimeEdit.dateChanged.connect(self.dateChanged) #日期变化信号绑定槽 这里到底是怎么传进函数里的date参数的？

self.datetimeEdit.timeChanged.connect(self.timeChanged) #时间变化信号绑定槽

self.datetimeEdit.dateTimeChanged.connect(self.dateTimeChanged) #日期或时间变化绑定槽

self.datetimeEdit2 = QDateTimeEdit(QDateTime.currentDateTime())

self.datetimeEdit2.setDisplayFormat('yyyy-MM-dd HH:mm:ss')

self.datetimeEdit3 = QDateTimeEdit(QDate.currentDate())

self.datetimeEdit3.setDisplayFormat('yyyy-MM-dd')

self.datetimeEdit4 = QDateTimeEdit(QTime.currentTime())

self.datetimeEdit4.setDisplayFormat('HH:mm:ss')

self.datetimeEdit5 = QDateTimeEdit()

self.datetimeEdit5.setCalendarPopup(True) #设置下拉框 弹出日历框 直接选择日期

self.datetimeEdit5.setDisplayFormat('yyyy-MM-dd HH:mm:ss')

self.button = QPushButton('获取日期和时间')

self.button.clicked.connect(self.buttonClicked)

layout = QVBoxLayout(self)

layout.addWidget(self.datetimeEdit)

layout.addWidget(self.datetimeEdit2)

layout.addWidget(self.datetimeEdit3)

layout.addWidget(self.datetimeEdit4)

layout.addWidget(self.datetimeEdit5)

layout.addWidget(self.button)

def dateChanged(self,date):

print(date) #PyQt5.QtCore.QDateTime(2001, 1, 1, 0, 0) #绑定信号和槽的时候，参数中self是类对象本身，

print(type(date)) #

def timeChanged(self,time):

print(time) #PyQt5.QtCore.QDate(2001, 1, 1)

def dateTimeChanged(self,dateTime):

print(dateTime)

def buttonClicked(self):

print(self.datetimeEdit.dateTime())

print(self.datetimeEdit.minimumDate())

print(self.datetimeEdit.maximumDateTime())

if __name__ == '__main__':

app = QApplication(sys.argv)

main = QDateTimeEditDemo()

main.show()

sys.exit(app.exec_())



#菜单QMenuBar

import sys

from PyQt5.QtWidgets import *

from PyQt5.QtGui import *

from PyQt5.QtCore import *

class MenuDemo(QMainWindow):

def __init__(self):

super(MenuDemo,self).__init__()

menu = QMenuBar(self)

file = menu.addMenu('文件')

file.addAction('新建')

save = QAction('保存',self)

save.setShortcut('Ctrl+S')

file.addAction(save)

save.triggered.connect(self.process)

edit = menu.addMenu('编辑')

edit.addAction('修改')

edit.addAction('删除')

edit.addAction('设置')

quit = QAction('退出',self)

edit.addAction(quit)

quit.triggered.connect(self.quit)

def process(self):

print(self.sender().text())

def quit(self):

self.close()

if __name__ == '__main__':

app = QApplication(sys.argv)

main = MenuDemo()

main.show()

sys.exit(app.exec_())



#创建和使用工具栏 工具栏三种显示状态：只显示图标，只显示文本，同时显示文本和图标，工具栏和菜单栏区别？菜单栏比较全，工具栏是提高效率响应快速，最好用图标

import sys

from PyQt5.QtWidgets import *

from PyQt5.QtGui import *

from PyQt5.QtCore import *

class ToolDemo(QMainWindow):

def __init__(self):

super(ToolDemo,self).__init__()

self.initUI()

def initUI(self):

self.setWindowTitle('工具栏演示')

self.resize(300,200)

tb1 = self.addToolBar('File')

tb1.addAction(QAction(QIcon('./images/new.png'),'new',self))

tb1.addAction(QAction(QIcon('./images/open.png'),'open',self))

tb1.addAction(QAction(QIcon('./images/save.png'),'save',self))

tb2 = self.addToolBar('Edit')

tb2.addAction(QAction(QIcon('./images/setting.png'), 'setting', self))

tb2.addAction(QAction(QIcon('./images/back.png'), 'back', self))

tb2.addAction(QAction(QIcon('./images/forward.png'), 'forward', self))

tb2.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) #图标后面加文字 setToolButtonStyle

tb1.actionTriggered.connect(self.toolbtnpressed)#绑定信号和槽 绑定的信号是 actionTriggered

tb2.actionTriggered.connect(self.toolbtnpressed) # 同时绑定一起，在后台看下结果

def toolbtnpressed(self,btn):

print('按下了',btn.text())

if __name__ == '__main__':

app = QApplication(sys.argv)

main = ToolDemo()

main.show()

sys.exit(app.exec_())



#状态栏 QStatusBar

import sys

from PyQt5.QtWidgets import *

from PyQt5.QtGui import *

from PyQt5.QtCore import *

class StatusBarDemo(QMainWindow):

def __init__(self):

super(StatusBarDemo,self).__init__()

self.initUI()

def initUI(self):

self.setWindowTitle('菜单栏的演示')

self.resize(300,200)

self.statusBar = QStatusBar() #实例化状态栏

self.setStatusBar(self.statusBar) #放在主窗口

menu = self.menuBar() #实例化一个菜单栏

file = menu.addMenu('File') #菜单栏里添加一个菜单

file.addAction('show') #菜单栏添加一个动作

file.triggered.connect(self.processTriggered)

def processTriggered(self,q):

if q.text()=='show':

self.statusBar.showMessage(q.text()+'被点击了',5000)

if __name__ == '__main__':

app = QApplication(sys.argv)

main = StatusBarDemo()

main.show()

sys.exit(app.exec_())



#显示对话框【没成功】

import sys

from PyQt5.QtWidgets import *

from PyQt5.QtGui import *

from PyQt5.QtCore import *

from PyQt5.QtPrintSupport import *

class PrintDemo(QWidget): #这里用QWidget，layout才有效，用QMainWindow无效

def __init__(self):

super(PrintDemo,self).__init__()

self.resize(400,300)

self.setWindowTitle('使用打印机')

self.button = QPushButton(self)

self.button.setText('点击打印内容')

self.button.clicked.connect(self.print)

self.textEdit = QTextEdit(self)

# self.textEdit.geometry()

layout = QVBoxLayout(self)

layout.addWidget(self.button)

layout.addWidget(self.textEdit)

def print(self):

printer = QPrinter()

painter = QPainter()

painter.begin(printer)

screen = self.textEdit.grab()

painter.drawPixmap(screen)

painter.end()

if __name__ == '__main__':

app = QApplication(sys.argv)

main = PrintDemo()

main.show()

sys.exit(app.exec_())